package com.baturu.vin.hyundai.dal.dao;

import com.baturu.vin.hyundai.CarTypeBuildTest;
import com.baturu.vin.hyundai.dal.entity.HyundaiKiaModelDetailEntity;
import com.baturu.vin.hyundai.dto.HyundaiAttributeEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.baturu.vin.hyundai.constant.HyundaiKiaTable.*;

public interface CarTypeBuildDAO {

    @Insert("insert into `" + T_CAR_SYSTEM_MAPPING + "` " +
            "(`epcBrand`, `region`, `area`, `segment`, `model`, `bpno`) " +
            "select d.`epcBrand`, d.`region`, d.`area`, d.`segment`, d.`model`, d.`bpno` " +
            "from `" + T_MODEL_DETAIL + "` d " +
            "left join `" + T_CAR_SYSTEM_MAPPING + "` m on d.`bpno` = m.`bpno` " +
            "where m.`id` is null ")
    void buildNewCarSystemMapping(@Param("version") String version);

    @Update("update `" + T_CAR_SYSTEM_MAPPING + "` m inner join `" + T_MODEL_DETAIL + "` d on m.`bpno` = d.`bpno` set " +
            "m.`epcBrand` = d.`epcBrand`, " +
            "m.`region` = d.`region`, " +
            "m.`area` = d.`area`, " +
            "m.`segment` = d.`segment`, " +
            "m.`model` = d.`model` ")
    void updateAllCarSystemMapping();

    @Select("select `id` from `" + T_CAR_SYSTEM_MAPPING + "` where `isIgnore` = 0 and " +
            "(`brand` = '' or `subBrand` = '' or `chName` = '' or `carSystemId` = 0)")
    List<Integer> queryUntreatedCarMappingIds();

    @Select("select `bpno` from `" + T_CAR_SYSTEM_MAPPING + "` where `isIgnore` = 0 order by id ")
    List<String> queryToBeTreatedBpnos();

    @Select("<script>" +
            "SELECT " +
            "`epcBrand`, `region`, `area`, `segment`, `model`, " +
            "`bpno`, `startYear`, `endYear`, `hsgb`, `attribute`, " +
            "`content` " +
            "FROM " + T_MODEL_DETAIL + " " +
            "WHERE bpno IN " +
            "<foreach item='item' index='index' collection='bpnos' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<HyundaiKiaModelDetailEntity> queryModelByBpno(@Param("bpnos") Collection<String> bpnos);

    @Update({"<script>" +
            "UPDATE `" + T_MODEL_DETAIL + "` d INNER JOIN (" +
            "<foreach item='item' index='index' collection='modelContents' open='' separator=' UNION ALL ' close=''>" +
            "select #{item.bpno} bpno, #{item.modelAttribute} attribute" +
            "</foreach>" +
            ") t on d.bpno = t.bpno " +
            "SET d.attribute = t.attribute "+
            "</script>"})
    void updateModelAttribute(@Param("modelContents") Collection<CarTypeBuildTest.ModelContentDTO> modelContents);

    @Select("<script>SELECT max(id) maxId, min(id) minId FROM `" + T_MINOR_ATTRIBUTE + "`</script>")
    Map<String, Long> queryMajorAttributeIdInfo();

    @Select({"<script>",
            "SELECT `id`, `bpno`, `type`, `code`, `describe`, `remark` FROM `" + T_MAJOR_ATTRIBUTE + "` ",
            "WHERE `remark` is null AND `id` &gt;= #{minId} AND `id` &lt;= #{maxId} ",
            "</script>"})
    List<HyundaiAttributeEntity> queryMajorAttributeByIdRange(@Param("minId") long minId, @Param("maxId") long maxId);

    @Update({"<script>" +
            "UPDATE `" + T_MAJOR_ATTRIBUTE + "` d INNER JOIN (" +
            "<foreach item='item' index='index' collection='attributes' open='' separator=' UNION ALL ' close=''>" +
            "select #{item.id} id, #{item.remark} remark" +
            "</foreach>" +
            ") t on d.`id` = t.`id` " +
            "SET d.`remark` = t.`remark` "+
            "</script>"})
    void updateMajorAttribute(@Param("attributes") Collection<HyundaiAttributeEntity> attributes);
}